class_name DotDebuff
extends BaseBuff

var buff_particle: GPUParticles2D = null
@onready var dot_timer: Timer = $DotTimer
@onready var duration_timer: Timer = $DurationTimer
var damage_per_second: float = 0
@export var debuff_particle_name: String = ""
	

func start_buff():
	var inst = (ResourceManager.get_resource(debuff_particle_name) as PackedScene).instantiate()
	add_child(inst)
	buff_particle = inst
	
	dot_timer.start()
	
	duration_timer.wait_time = duration
	duration_timer.start()


func _on_poison_dot_timeout() -> void:
	var hit_data = HitData.new()
	hit_data.is_critical = true
	
	var damage = ceil(holder.unit_data.max_hp * damage_per_second) 
	hit_data.is_direct_damage = true
	hit_data.direct_damage = damage
	
	hit_data.knockback = 0
	
	if is_instance_valid(sender):
		holder.take_hit(hit_data, sender)
	else:
		holder.take_hit(hit_data, null)


func _on_poison_duration_timer_timeout() -> void:
	dot_timer.stop()
	buff_particle.emitting = false
	await get_tree().create_timer(1.0).timeout
	queue_free()
